package cn.flc.jsonresult;


import lombok.Data;

/**
 * 接口统一返回结果对象
 * @author wujiangbo
 * @date 2022-08-28 15:16
 */
@Data
public class JSONResult {

    private boolean success = true;

    private String message = "成功";

    //成功统一返回200，其余编码全部是错误码
    private String code = "200";

    //返回的数据
    private Object data;

    //创建当前实例
    public static JSONResult success(){
        return new JSONResult();
    }

    //创建当前实例
    public static JSONResult success(Object obj){
        JSONResult instance = new JSONResult();
        instance.setData(obj);
        return instance;
    }
    public static JSONResult success(String message){
        JSONResult instance = new JSONResult();
        instance.setMessage(message);
        return instance;
    }

    //成功，但是返回不同消息代码
    public static JSONResult success(Object obj, String code){
        JSONResult instance = new JSONResult();
        instance.setSuccess(true);
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }

    //创建当前实例
    public static JSONResult error(){
        JSONResult instance = new JSONResult();
        instance.setCode("500");
        instance.setSuccess(false);
        instance.setMessage("失败");
        return instance;
    }

    //创建当前实例
    public static JSONResult error(String message){
        JSONResult instance = new JSONResult();
        instance.setCode("500");
        instance.setSuccess(false);
        instance.setMessage(message);
        return instance;
    }

    public static JSONResult error(String message, Object obj){
        JSONResult instance = new JSONResult();
        instance.setCode("500");
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setData(obj);
        return instance;
    }

}